package net.itheima.mm.aop;

/**
 * @author zhangyan
 * @date 2021/2/26 0:27
 */

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@Aspect
@Component
public class AopAdvice {

    @Pointcut("execution(* net.itheima.mm.service..*.*(..))")
    public void Pointcut(){}

    @Before("Pointcut()")
    public void before(JoinPoint joinPoint){
        //执行的时间
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String dateStr = sdf.format(now);
        //获取方法的名称
        Signature signature = joinPoint.getSignature();
        //方法的参数
        Object[] args = joinPoint.getArgs();

        System.out.println(dateStr  + " --- " + signature.toString() + "【" + Arrays.toString(args)+ "】");

    }

    @AfterReturning(value = "Pointcut()",returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result){
        //执行的时间
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String dateStr = sdf.format(now);
        //获取方法的名称
        Signature signature = joinPoint.getSignature();
        //获取方法的返回值

        System.out.println(dateStr  + " --- " + signature.toString() + "{" +  result +"}");

    }
}

